package com.green.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 处理季度时间
 * 
 * @author Administrator
 * 
 */
public class DateUtils {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	private static String QONE = "QONE";
	private static String QTWO = "QTWO";
	private static String QTHREE = "QTHREE";
	private static String QFOUR = "QFOUR";

	/**
	 * 根据年月得到这个月最后一天
	 * 
	 * @param year
	 * @param months
	 * @return
	 */
	public  String getDateStr(int year, int months) {
		sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		// 不加下面2行，就是取当前时间前一个月的第一天及最后一天
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, months);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		Date lastDate = cal.getTime();
		return sdf.format(lastDate);
	}

	
	
	/**
	 * 得到当前月的第一天
	 * @return
	 */
	public String getNowMonthDay(){
		sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return sdf.format(cal.getTime());
	}
	
	
	/**
	 * 根据所传季度查询季度区间
	 * 
	 * @return
	 */
	public String[] getDateQ(String year, String qStr) {
		String[] dataStr = new String[2];
		if (qStr.equals(QONE)) {
			dataStr[0] = year + "-01";
			dataStr[1] = year + "-03";
		} else if (qStr.equals(QTWO)) {
			dataStr[0] = year + "-04";
			dataStr[1] = year + "-06";
		} else if (qStr.equals(QTHREE)) {
			dataStr[0] = year + "-07";
			dataStr[1] = year + "-09";
		} else if (qStr.equals(QFOUR)) {
			dataStr[0] = year + "-10";
			dataStr[1] = year + "-12";
		}
		return dataStr;
	}

	/**
	 * 得到前半年，后半年开始时间和结束时间
	 * @param year
	 * AGO 前半年 AFTER 后半年
	 * @param yStr
	 * @return
	 */
	public String[] getDateYear(String year,String yStr){
		String[] dataStr = new String[2]; 
		if(yStr.equals("AGO")){
			dataStr[0] = year + "-01";
			dataStr[1] = year + "-06";
		}else if(yStr.equals("AFTER")){
			dataStr[0] = year + "-07";
			dataStr[1] = year + "-12";
		}
		return dataStr;
	}
	
	/**
	 * 根据所传年度
	 * 
	 * @return
	 */
	public String[] getDateYear(String year) {
		String[] dataStr = new String[2];
		dataStr[0] = year + "-01";
		dataStr[1] = year + "-12";
		return dataStr;
	}

	/**
	 * 得到当前月份的前一年所有月份
	 * 
	 * @return
	 */
	public String[] getDateYear() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, -1);
		String time = new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String[] timeStr = time.split("-");
		String[] dataStr = new String[12];
		String year = "";
		String months = "";
		int j = 1;
		for (int i = 0; i < 12; i++) {
			if (Integer.parseInt(timeStr[1]) + (i+1) <= 12) {
				year = timeStr[0];
				if (Integer.parseInt(timeStr[1]) + (i+1) < 10) {
					months = "0"
							+ String.valueOf(Integer.parseInt(timeStr[1]) + (i+1));
				} else {
					months = String.valueOf(Integer.parseInt(timeStr[1]) + (i+1));
				}
				dataStr[i] = year + "-" + months;
			} else {
				year = String.valueOf(Integer.parseInt(timeStr[0])+1);
				if (j < 10) {
					months = "0" + j;
				} else {
					months = String.valueOf(j);
				}
				dataStr[i] = year + "-" + months ;
				j++;
			}
		}
		return dataStr;
	}
	
	
	
	public static void main(String[] args){
		DateUtils dd=new DateUtils();
		System.out.println(dd.getNowMonthDay());
	}
}
